/**
 * Created by zhang_renyang on 14/12/31.
 */
var express = require('express');
var router = express.Router();
var Article = require('../model/article');
var Category = require('../model/category');
var settings = require('../settings');

/**
 *后台分页查询文章
 */
router.get('/list/:pageNum/:pageSize',function(req,res){
    var pageNum = req.params.pageNum?req.params.pageNum:1;
    var pageSize = req.params.pageSize?req.params.pageSize:settings.pageSize;
    Article.pageQuery({},{pageNum:pageNum,pageSize:pageSize},function(err,count,articles){
        if(err){
           next(err);
        }else{
            var totalPage = Math.ceil(count/pageSize);
            res.render('article',{
                user: req.session.user,
                pageNum:pageNum,
                pageSize:pageSize,
                totalPage:totalPage,
                articles:articles
            });
        }

    });
});

router.get('/view/:articleId',function(req,res){
    Article.findById(req.params.articleId,function(err,article){
        Category.tree(function(err,categories){
            if(err){
                req.flash('error',err);
                res.redirect('back');
            }else{
                res.render('articleDetail',{
                    categories:categories,
                    article:article,
                    user: req.session.user,
                    success: req.flash('success').toString(),
                    error: req.flash('error').toString()});
            }
        });
    });
});


/**
 * 跳转到增加文章页面
 */
router.get('/add',function(req,res){
    Category.tree(function(err,categories){
        res.render('newArticle',{
            user: req.session.user,
            method:'add',
            article:{},
            categories:categories});
    });
});

/**
 *增加文章
 */
router.post('/add',function(req,res,next){
   var newArticle = new Article({
       articleTitle : req.body.articleTitle,
       order_by:req.body.order_by,
       user_id:req.body.user_id,
       is_recommend:req.body.is_recommend,
       functionDescription : req.body.functionDescription,
       articleExamples : req.body.articleExamples,
       jobInterview : req.body.jobInterview
   });
   newArticle.save(function(err,category){
       console.error(err);
       if(err){
           req.flash('error',err);
           res.redirect('back');
       }else{
           req.flash('success',"添加成功!");
           res.redirect('/article/list/1/'+settings.pageSize);
       }
    });
});

/**
 *跳转到编辑文章页面
 */
router.get('/edit/:articleId',function(req,res){
    Article.findById(req.params.articleId,function(err,article){
        Category.tree(function(err,categories){
            if(err){
                req.flash('error',err);
                res.redirect('back');
            }else{
                res.render('newArticle',{
                    user: req.session.user,
                    article:article,
                    method:'edit',
                    categories:categories
                });
            }
        });
    });
});

/**
 *编辑文章
 */
router.post('/edit',function(req,res){
    var article = {
            articleTitle:req.body.articleTitle,
            order_by:req.body.order_by,
            is_recommend:req.body.is_recommend,
            category_id:req.body.category_id,
            functionDescription:req.body.functionDescription,
            articleExamples:req.body.articleExamples,
            jobInterview:req.body.jobInterview
    };
    var newArticle = new Article(article);
    newArticle.update(req.body._id,function(err,article){
        if(err){
            req.flash('error',err);
            res.redirect('back');
        }else{
            req.flash('success',"修改成功!");
            res.redirect('/article/list/1/'+settings.pageSize);
        }
    });
});

/**
 *删除文章
 */
router.get('/delete/:articleId',function(req,res){
    Article.deleteById(req.params.articleId,function(err){
        if(err){
            req.flash('error',err);
            res.redirect('back');
        }
        req.flash('success',"删除成功!");
        res.redirect('/article/list/1/'+settings.pageSize);
    });
});

module.exports = router;